﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.SqlServer.Server;
using OpenSoftStore.BusinessLogic.Provider;
using OpenSoftStore.Orm.Model;
using OpenSoftStore.Client.UIL.ManageCommon;

namespace OpenSoftStore.Client.UIL.ManageProvider
{
    public partial class ProviderAdd : UserControl
    {
        #region Fields

        public T_Provider provider;
        public Boolean Drog = false;
        private const string _ShopId = "b0def1a7-bf25-45f2-b65a-6dc586dbfc2c";
        public int Top = 0;
        public int Left = 0;

        #endregion
        public ProviderAdd()
        {
            provider = new T_Provider();
            InitializeComponent();
        }

        public void GetProvider(string id)
        {
            provider.Id = id;
            var providerBll = new ProviderLogic();
            var data = providerBll.GetProviderView(" where P.Id='" + id + "'").FirstOrDefault();

            txtRemark.Text = data.Remark;
            txtProviderName.Text = data.Name;
            txtMobile.Text = data.Mobile;
            txtQQ.Text = data.Qq;
            txtProviderType.Text = data.TreeName;
            txtTel.Text = data.Tel;
            txtAddress.Text = data.Address;
            txtBankCard.Text = data.BankCard;
            txtOpenBank.Text = data.OpenBank;
            txtContact.Text = data.Contact;
            txtEmail.Text = data.Email;
            txtFax.Text = data.Fax;
        }

        public void SetTreeValue(string treeId, string treeName)
        {
            txtProviderType.Text = treeName;
            provider.TreeId = treeId;
        }

        private void toolStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }

        private void btShop_Click(object sender, EventArgs e)
        {
            var formTreeSelecter = new FormTreeSelecter();
            formTreeSelecter.GetTreeByType("ProviderTree");
            formTreeSelecter.ShowDialog();
            if (formTreeSelecter.DialogResult != DialogResult.OK) return;
            provider.TreeId = formTreeSelecter.TypeId;
            txtProviderType.Text = formTreeSelecter.TypeText;
        }

        private void BtnSave_Click(object sender, EventArgs e)
        {

        }

        private void glassButton2_Click(object sender, EventArgs e)
        {

        }

        private void pbClose_Click(object sender, EventArgs e)
        {
            var findForm = FindForm();
            if (findForm == null) return;
            findForm.DialogResult = DialogResult.Cancel;
            findForm.Close();
        }

        private void glassButton1_Click(object sender, EventArgs e)
        {
            var findForm = FindForm();
            if (findForm == null) return;
            findForm.DialogResult = DialogResult.Cancel;
            findForm.Close();
        }

        private void glassButton3_Click(object sender, EventArgs e)
        {
            var providerBll = new ProviderLogic();
            provider.Name = txtProviderName.Text.Trim();
            if (string.IsNullOrEmpty(provider.Name))
            {
                MessageBox.Show("供应商名称不能为空！");
                return;
            }
            else if (string.IsNullOrEmpty(txtProviderType.Text.Trim()))
            {
                MessageBox.Show("供应商类型不能为空！");
                return;
            }
            provider.Mobile = txtMobile.Text.Trim();
            provider.OpenBank = txtOpenBank.Text.Trim();
            provider.Qq = txtQQ.Text.Trim();
            provider.ShopId = _ShopId;
            provider.Tel = txtTel.Text.Trim();
            provider.Fax = txtFax.Text.Trim();
            provider.Address = txtAddress.Text.Trim();
            provider.BankCard = txtBankCard.Text.Trim();
            provider.Contact = txtContact.Text.Trim();
            provider.Email = txtEmail.Text.Trim();
            provider.Remark = txtRemark.Text.Trim();
            if (provider.Id == null)
            {
                providerBll.Add(provider);
            }
            else
            {
                if (provider.Id == "46872df4-47b1-4875-bdbb-1f8f8d1023ee")
                {
                    FormSysMessage.ShowMessage("此数据为系统基础数据，不能修改！");
                    return;
                }
                providerBll.Update(provider);
            }
            ProviderList.Instance.button7_Click(null, null);
            var findForm = FindForm();
            if (findForm != null) findForm.Close();
        }

        private void PnlInfoTitle_MouseDown(object sender, MouseEventArgs e)
        {
            Drog = true;
            Top = e.Y;
            Left = e.X;
        }

        private void PnlInfoTitle_MouseMove(object sender, MouseEventArgs e)
        {
            if (!Drog) return;
            var findForm = FindForm();
            if (findForm == null) return;
            findForm.Top += e.Y - Top;
            findForm.Left += e.X - Left;
        }

        private void PnlInfoTitle_MouseUp(object sender, MouseEventArgs e)
        {
            Drog = false;
        }

    }
}
